<?php

/**
 * 轮播图
 */

namespace app\api\controller;

use app\common\controller\ApiController;
use custom\Err;
use think\Exception;
use app\api\model\Dynamic as DynamicModel;
use app\common\lib\RespCode;

class Dynamic  extends ApiController {

    protected $notNeedToken = [];

    protected $isArrayResult = [];

    /**
     * 经纪人动态列表
     * @return false|string
     */
    public function getDynamicList(){
        try {
            $params = $this->checkParams ('page');
            $DynamicModel = new DynamicModel();
            $pageSize=10;
            $data = $DynamicModel->getDynamicList($params,$pageSize);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     *动态保存
     * @return false|string
     */
    public function saveDynamic(){
        try {
            $params = $this->checkParams ('');
            if (isset($params['pics'])&&$params['pics']){
                $pic=explode(',',$params['pics']);
                $count=count($pic);
                if ($count>9){
                    Err::add('最多上传9张图片',RespCode::PARAMS_ERR);
                    return $this->resErr ();
                }
            }
            $DynamicModel = new DynamicModel();
            $data = $DynamicModel->saveDynamic ($params);
            if ($data){
                return $this->resData ('success', []);
            }else{
                return $this->resErr ();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 动态删除
     * @return false|string
     */
    public function delDynamic(){
        try {
            $params = $this->checkParams ('trends_id');
            $DynamicModel = new DynamicModel();
            $data = $DynamicModel->delDynamic ($params);
            if ($data){
                return $this->resData ('success', []);
            }else{
                throw new Exception('操作失败', RespCode::QUERY_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }


}
